package com.acegear.horizon.domain.models.jpa;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

import java.util.Optional;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 * Created by wangsike on 2016/12/10.
 */
@Entity
public class WechatAuth {

    private Long userId;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private Long id;

    @Column(columnDefinition = "VARCHAR(100)")
    private String unionId;

    @Column(unique = true, columnDefinition = "VARCHAR(100)")
    private String openId;

    private Integer type;

    public WechatAuth() {
    }

    public WechatAuth(Long userId, String unionId, String openId, Integer type) {
        this.userId = userId;
        this.unionId = unionId;
        this.openId = openId;
        this.type = type;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUnionId() {
        return unionId;
    }

    public void setUnionId(String unionId) {
        this.unionId = unionId;
    }

    public String getOpenId() {
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Optional<UserInfo> auth(String token) {
        String wechatUrl = "https://api.weixin.qq.com/sns/userinfo";

        WechatUserInfo wechatUserInfo = null;

        try {
            wechatUserInfo = Unirest.get(wechatUrl).queryString("openid", openId).queryString("access_token", token).asObject(WechatUserInfo.class).getBody();
        } catch (UnirestException e) {
            e.printStackTrace();
        }

        if (wechatUserInfo == null || wechatUserInfo.isError()) {
            return Optional.empty();
        } else {
            UserInfo info = new UserInfo();
            info.setNickname(wechatUserInfo.getNickname());
            info.setAvatar(wechatUserInfo.getHeadimgurl());
            info.setGender(wechatUserInfo.getSex());
            info.setProvince(wechatUserInfo.getProvince());
            return Optional.of(info);
        }
    }
}

@JsonIgnoreProperties(ignoreUnknown = true)
class WechatUserInfo {
    private String nickname;
    private String headimgurl;
    private String unionid;
    private Integer sex;
    private String province;
    private String city;
    private String country;
    private Integer errcode;
    private String errmsg;

    public WechatUserInfo() {

    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getHeadimgurl() {
        return headimgurl;
    }

    public void setHeadimgurl(String headimgurl) {
        this.headimgurl = headimgurl;
    }

    public String getUnionid() {
        return unionid;
    }

    public void setUnionid(String unionid) {
        this.unionid = unionid;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public Integer getErrcode() {
        return errcode;
    }

    public void setErrcode(Integer errcode) {
        this.errcode = errcode;
    }

    public String getErrmsg() {
        return errmsg;
    }

    public void setErrmsg(String errmsg) {
        this.errmsg = errmsg;
    }

    public boolean isError() {
        return errcode != null;
    }

    @Override
    public String toString() {
        return "WechatUserInfo{" +
                "nickname='" + nickname + '\'' +
                ", headimgurl='" + headimgurl + '\'' +
                ", unionid='" + unionid + '\'' +
                ", sex=" + sex +
                ", province='" + province + '\'' +
                ", city='" + city + '\'' +
                ", country='" + country + '\'' +
                '}';
    }
}